home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11491 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  55 lines

  1. Path: vixen.cso.uiuc.edu!usenet
  2. From: homer@uiuc.edu
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: template with unknown classes!
  5. Date: 14 Mar 1996 20:09:39 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4i9ue3$9g0@vixen.cso.uiuc.edu>
  8. References: <3146F7D0.791D@nada.kth.se> <4i9gto$dtu@uuneo.neosoft.com>
  9. Reply-To: n-dade@uiuc.edu
  10. NNTP-Posting-Host: homer.apr.uiuc.edu
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. Wmatthew@lan-aces.com (W. Matthews) writes:
  14. >In article <3146F7D0.791D@nada.kth.se>, Andreas Swdrdh <da1-asw@nada.kth.se> says:
  15.  
  16. >>// any success yet. The problem is that I don't want the whole class to be a
  17. >>// template, just THIS function. Is there a solution with templates or do 
  18. >>// you have to use friend in some sort of way? Maybe use of dummy-class-pointer
  19. >>// just to get the right class type?! Would be most greatful for fast replies.
  20. >>
  21. >>// The possible function?!
  22. >>
  23. >>template<class object>void new_object()   // or something like that!
  24. >>{
  25. >>  if(!head)
  26. >>    head = new object;  // where object doesn't have to be known.
  27. >>  ...                   // And the constuctor of object initializes it!
  28. >>  ...
  29. >>}
  30.  
  31. Have two classes: the first is a non-template class that contains everything
  32. about a listhandle except the new_object() function. Derive from that a
  33. template class that contains the new_object() function. ie
  34.  
  35. class listhandlebase {
  36.       void*    head;
  37.    public:
  38.       listhandlebase() : head(NULL) {}
  39.    ... all the other functions you want...
  40. };
  41.  
  42. template<class OBJECT> class listhandle : public listhandlebase {
  43.    public:
  44.       new_object() {
  45.          if (!head) { head = new OBJECT; }
  46.       }
  47. };
  48.  
  49. This way all the different listhandle classes share the code in listhandlebase
  50. (which I assume was the point of not wanting make the entire listhandle
  51. class a template class).
  52.  
  53. -Nicolas Dade / n9rzb / nicolas-dade@uiuc.edu
  54.  
  55.